package com.adcc.loadsheet.billing.controller;

import com.adcc.aoc.devLib.common.log.Log;
import com.adcc.loadsheet.billing.service.SystemConfigService;
import com.adcc.loadsheet.billing.util.LogUtil;
import com.adcc.loadsheet.billing.vo.ResultStatus;
import com.adcc.loadsheet.billing.vo.ResultVO;
import com.adcc.loadsheet.billing.vo.SystemConfigEntityVO;
import com.google.common.base.Strings;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 自动处理帐单Controller层
 * Created by zhaoml on 2020/9/4.
 */
@Controller
@RequestMapping(value = "automaticprocessingbills")
@Api(value = "/automaticprocessingbills", description = "自动处理帐单相关操作controller")
public class AutomaticProcessingBillsController extends BasisController{

    //生成账单时间在配置表里的key
    private String automaticProcessingBillsTimeKey = "automaticProcessingBillsTime";

    @Autowired
    SystemConfigService systemConfigService;

    //跳转到自动处理帐单页面
    @PreAuthorize("hasPermission('','AutomaticProcessingBills')")
    @GetMapping(value = "toautomaticprocessingbills")
    @ApiOperation(value = "/toautomaticprocessingbills", notes = "跳转到自动处理帐单页面", httpMethod = "GET")
    public String toAutomaticProcessingBills(Model model) {
        //时间列表
        String[] timeList = {"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15",
                "16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
        model.addAttribute("timeList", timeList);
        //查询生成账单时间
        SystemConfigEntityVO systemConfigVO = systemConfigService.getSystemConfig(automaticProcessingBillsTimeKey);
        model.addAttribute("automaticProcessingBillsTime", (systemConfigVO == null || Strings.isNullOrEmpty(systemConfigVO.getConfigValue())) ? "01" : systemConfigVO.getConfigValue());
        return "automaticProcessingBills/automaticprocessing-bills";
    }

    /**
     * 设置生成账单时间
     *
     * @param systemConfigEntityVO
     * @return
     * @throws Exception
     */
    @PostMapping(value = "setAutomaticProcessingBillsTime", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    @ApiOperation(value = "/setAutomaticProcessingBillsTime", notes = "设置生成账单时间", httpMethod = "POST")
    @ResponseBody
    public ResultVO setAutomaticProcessingBillsTime(@Valid @RequestBody SystemConfigEntityVO systemConfigEntityVO) throws Exception {
        ResultVO vo = new ResultVO();
        try {
            systemConfigEntityVO.setConfigKey(automaticProcessingBillsTimeKey);
            String res = systemConfigService.edit(systemConfigEntityVO);
            vo.setStatus(ResultStatus.SUCCESS);
            vo.getData().put("msg", res);
        } catch (Exception ex) {
            Log.error(ContractUserController.class, "设置生成账单时间出错", ex);
            vo.setStatus(ResultStatus.FAILED);
            vo.setErrorReason("设置生成账单时间出错");
            LogUtil.errorLog(ContractUserController.class, ex, "setAutomaticProcessingBillsTime()");
        }
        return vo;
    }
}